package com.graduate.daydayup.activity

import android.content.Intent
import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.LogUtils
import com.graduate.daydayup.R
import com.graduate.daydayup.adpter.NoteAdapter
import com.graduate.daydayup.base.BaseActivity
import com.graduate.daydayup.event.EventMessage
import com.graduate.daydayup.event.REFRESH_NOTE
import com.graduate.daydayup.http.HotBean
import com.graduate.daydayup.room.NoteContent
import com.graduate.daydayup.room.noteContentDao
import kotlinx.android.synthetic.main.activity_note.*
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

/**
 * aburther:朱礼尧 联系qq1287587036
 */
class NoteActivity : BaseActivity() {

    private val noteAdapter:NoteAdapter= NoteAdapter(R.layout.layout_note)
    override fun setTitle()="课程便签"

    override fun getLayout()= R.layout.activity_note

    override fun setTextRight()=""

    override fun setImgRightListener() {

    }

    override fun isAddToolbar()=true

    override fun initViewIds() {

    }

    //接收消息
    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onMessageEvent(event: EventMessage) {

        when(event.code){
          REFRESH_NOTE->getData()
        }
    }

    override fun initView() {
        EventBus.getDefault().register(this)
        rl_addtype.setOnClickListener { ActivityUtils.startActivity(NoteTypeAddActivity::class.java) }
        rl_addcontent.setOnClickListener { ActivityUtils.startActivity(NoteContentAddActivity::class.java) }
    }

    override fun initData(savedInstanceState: Bundle?) {
        val allNotes = noteContentDao?.getAllNotes()
        rv_notes.adapter=noteAdapter
        rv_notes.layoutManager=LinearLayoutManager(this)
        if (allNotes != null) {
            noteAdapter.setList(allNotes)
        }

        noteAdapter.setOnItemClickListener { adapter, view, position ->
            val noteContent = adapter.getItem(position) as NoteContent
            val intent = Intent(applicationContext, NoteContentEditActivity::class.java)
                intent.putExtra("id",noteContent.id)
                startActivity(intent)



        }
    }

    private fun getData(){
        LogUtils.e("收到消息")
        val allNotes = noteContentDao?.getAllNotes()
        noteAdapter.setList(allNotes)
        noteAdapter.notifyDataSetChanged()
    }

    override fun onDestroy() {
        super.onDestroy()
        EventBus.getDefault().unregister(this)
    }
}